package cn.glutnn.console;

import java.util.List;
import java.util.Scanner;

import cn.glutnn.dao.NoticeDao;
import cn.glutnn.dao.RoomDao;
import cn.glutnn.dao.SeatDao;
import cn.glutnn.dao.TicketDao;
import cn.glutnn.entity.Notice;
import cn.glutnn.entity.Room;
import cn.glutnn.entity.Seat;
import cn.glutnn.entity.Ticket;

public class TicketConsole {
	
	NoticeDao noticeDao = new NoticeDao();
	RoomDao roomDao = new RoomDao();
	SeatDao seatDao = new SeatDao();
	TicketDao ticketDao = new TicketDao();
	
	public void fn售票功能() {
		// 1、要求录入票据信息
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入顾客手机：");
		String tel = scanner.next();
		
		System.out.println("请输入电影名称：");
		String noticename = scanner.next();
		
		System.out.println("请输入影厅名称：");
		String roomname = scanner.next();
		
		System.out.println("请输入影厅座位编号：");
		String seatcode = scanner.next();
		
		// 2、验证是否能出票
		Notice notice = noticeDao.findOne(noticename);
		if (notice==null) {
			System.out.println("找不到该《"+noticename+"》电影,不能出票");
			return;
		}
		
		Room room = roomDao.findOne(roomname);
		if (room == null) {
			System.out.println("找不到该“"+roomname+"”影厅,不能出票");
			return;
		}
		
		Seat seat = seatDao.findOneByRoomidAndCode(room.id, seatcode);
		if (seat == null) {
			System.out.println("找不到该“"+roomname+"”座位,不能出票");
			return;
		} else {
			if(seat.isuse) {
				System.out.println("该“"+roomname+"”座位已占用,不能出票");
				return;
			}
		}
		
		// 3、保存票据信息
		
		Ticket ticket = new Ticket(tel, noticename, roomname, seatcode, "张三");
		boolean isOK = ticketDao.save(ticket);
		
		// 3-1、设置座位信息已占用
		seat.isuse = Boolean.TRUE;
		boolean isUpdateSeatOK = seatDao.update(seat);
		
		// 4、友好提示
		System.out.println(isOK && isUpdateSeatOK ? "保存票据成功" : "保存票据失败");
	}
	
	
	/**
	 * 票据打印
	 */
	public void print() {
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入需要打印票的顾客手机：");
		String tel = scanner.next();
		
		List<Ticket> list = ticketDao.findByTel(tel);
		System.out.println("==========");
		for(Ticket ticket: list) {
			System.out.println("票信息：");
			System.out.println("“《"+ticket.noticename+"》” ，影厅(座位)："+ticket.roomname+"("+ticket.seatcode+")");
		}
		System.out.println("==========");
	}

}
